package view;

import flyingpig.com.R;
import graphics.BackgroundScreen;

import java.util.ArrayList;

import models.GeneralObject;
import models.Mushroom;
import models.Pig;
import models.Wolf;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 *
 * @author Merete, Mirna, Esben, Knut og Stian
 *
 */
public class MainView extends SurfaceView implements SurfaceHolder.Callback {

	public static final String TAG = MainView.class.getSimpleName();
	private static MainView mgp;
	private MainThread thread;
	private ArrayList<GeneralObject> generalObjectList;
	private BackgroundScreen bgscreen;
	private Wolf wolf0, wolf1, wolf2, wolf3, wolf4, wolf5, wolf6;
	private Mushroom shroom0, shroom1, shroom2, shroom3, shroom4, shroom5,
			shroom6;
	private Pig piggy;

	private MainView(Context context) {
		super(context);
		getHolder().addCallback(this);
		thread = MainThread.getInstance(getHolder(), this);
		setFocusable(true);
	}

	// Singleton implementation
	public static synchronized MainView getInstance(Context context) {
		if (mgp == null) {
			return mgp = new MainView(context);
		} else {
			return mgp;
		}

	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		/*
		 * thread.setRunning(false); ((Activity)getContext()).finish();
		 */
		// TODO Auto-generated method stub

	}

	public void surfaceCreated(SurfaceHolder holder) {
		// Make objects
		generalObjectList = new ArrayList<GeneralObject>();
		bgscreen = new BackgroundScreen(BitmapFactory.decodeResource(
				getResources(), R.drawable.background2), 0, 0);

		wolf0 = new Wolf(BitmapFactory.decodeResource(getResources(),
				R.drawable.wulfy), -100, -100, getWidth(), getHeight());
		wolf1 = new Wolf(BitmapFactory.decodeResource(getResources(),
				R.drawable.wulfy), -100, -100, getWidth(), getHeight());
		wolf2 = new Wolf(BitmapFactory.decodeResource(getResources(),
				R.drawable.wulfy), -100, -100, getWidth(), getHeight());
		wolf3 = new Wolf(BitmapFactory.decodeResource(getResources(),
				R.drawable.wulfy), -100, -100, getWidth(), getHeight());
		wolf4 = new Wolf(BitmapFactory.decodeResource(getResources(),
				R.drawable.wulfy), -100, -100, getWidth(), getHeight());
		wolf5 = new Wolf(BitmapFactory.decodeResource(getResources(),
				R.drawable.wulfy), -100, -100, getWidth(), getHeight());
		wolf6 = new Wolf(BitmapFactory.decodeResource(getResources(),
				R.drawable.wulfy), -100, -100, getWidth(), getHeight());

		// make shrooms!
		shroom0 = new Mushroom(BitmapFactory.decodeResource(getResources(),
				R.drawable.smallshrrom), 0, 0, getWidth(), getHeight());

		Log.d(TAG, "Generert shroom");

		piggy = new Pig(BitmapFactory.decodeResource(getResources(),
				R.drawable.smallpig), 160, 240, getWidth(), getHeight());

		// add objects to the objectlist
		generalObjectList.add(piggy);
		generalObjectList.add(wolf0);
		generalObjectList.add(wolf1);
		generalObjectList.add(wolf2);
		generalObjectList.add(wolf3);
		generalObjectList.add(wolf4);
		generalObjectList.add(wolf5);
		generalObjectList.add(wolf6);
		generalObjectList.add(shroom0);

		// start the main thread
		thread.initPainterAndGLH(generalObjectList);
		thread.setRunning(true);
		thread.start();

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		if (event.getY() > getHeight() - 50) {
			thread.setRunning(false);
			((Activity) getContext()).finish();
		} else {
			piggy.handleMotionEvent(event);
		}

		return true;
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again to shut down the thread
			}
		}
		Log.d(TAG, "Thread was shut down cleanly");

	}

}
